package com.xuelang.pipeline.client.pipeline.utils;

import com.xuelang.pipeline.client.pipeline.enums.WeekDayEnum;

/**
 * <p>Cron表达式工具类</p>
 *
 * @author : yixin.lys
 * @date : 2024-06-12 16:54
 **/
public class CronExpressionUtils {

    public static String generateCronExpression(WeekDayEnum weekday, String time) {
        // Validate time format
        if (!time.matches("\\d{2}:\\d{2}:\\d{2}")) {
            throw new IllegalArgumentException("Invalid time format. Expected format is HH:mm:ss");
        }

        // Split time into hours, minutes, and seconds
        String[] timeParts = time.split(":");
        String hour = timeParts[0];
        String minute = timeParts[1];
        String second = timeParts[2];

        // Get the corresponding value for the weekday
        String dayOfWeekValue = weekday.name();

        // Generate the cron expression
        String cronExpression = String.format("%s %s %s ? * %s", second, minute, hour, dayOfWeekValue);
        return cronExpression;
    }

    public static void main(String[] args) {
        WeekDayEnum weekday = WeekDayEnum.MON;
        String time = "09:33:41";
        String cronExpression = generateCronExpression(weekday, time);
        System.out.println(cronExpression); // Output: 41 33 09 ? * 1
    }

}
